package com.iotek.bookstore.modle.dao.impl;

import com.iotek.bookstore.modle.entity.Address;
import com.iotek.bookstore.util.PreparedStatementSetter;
import com.iotek.bookstore.util.RsCallBackHandler;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class AddressDaoImpl extends MyBookstoreDaoImpl<Address> {

    /*
    * 添加收货人地址
    * */
    @Override
    public int insert(final Address address) {
        String sql="insert into address (addressId,userId,name,site,postcode,phone,mobilePhone) values(?,?,?,?,?,?,?)";
        return this.update(sql, new PreparedStatementSetter() {
            public void setValues(PreparedStatement pstmt) throws SQLException {
                pstmt.setLong(1,address.getAddressId());
                pstmt.setInt(2,address.getUserId());
                pstmt.setString(3,address.getName());
                pstmt.setString(4,address.getSite());
                pstmt.setString(5,address.getPostcode());
                pstmt.setString(6,address.getPhone());
                pstmt.setString(7,address.getMobilePhone());
            }
        });
    }

    /*
    * 查询所有用户的地址信息
    * */
    @Override
    public List<Address> queryAll() {
        String sql="select * from address";
        this.query(sql, null, new RsCallBackHandler() {
            public void processRs(ResultSet rs) throws SQLException {
                createAddress(rs);
            }
        });
        return list;
    }

    /*
    * 根据用户ID查询该用户的所有收件人地址信息或者根据地址ID查找一条收件人地址信息
    * */
    @Override
    public List<Address> query(final long id,String type) {
        if(type.equals("userId")){
            //根据用户ID查询该用户的所有收件人地址信息
            String sql="select * from address where userId=?";
            this.query(sql, new PreparedStatementSetter() {
                public void setValues(PreparedStatement pstmt) throws SQLException {
                    pstmt.setLong(1, id);
                }
            }, new RsCallBackHandler() {
                public void processRs(ResultSet rs) throws SQLException {
                    createAddress(rs);
                }
            });
        }else if(type.equals("addressId")){
            //根据地址ID查找一条收件人地址信息
            String sql="select * from address where addressId=?";
            this.query(sql, new PreparedStatementSetter() {
                public void setValues(PreparedStatement pstmt) throws SQLException {
                    pstmt.setLong(1, id);
                }
            }, new RsCallBackHandler() {
                public void processRs(ResultSet rs) throws SQLException {
                    createAddress(rs);
                }
            });
        }
        return list;
    }

    /*
    * 根据地址ID删除地址信息
    * */
    @Override
    public int delete(final long id) {
        String sql="delete from address where addressId=?";
        return this.update(sql, new PreparedStatementSetter() {
            public void setValues(PreparedStatement pstmt) throws SQLException {
                pstmt.setLong(1,id);
            }
        });
    }


    /*
    * 根据地址ID更改地址信息
    * */
    @Override
    public int update(final Address address) {
        String sql="update address set name=?,site=?,postcode=?,phone=?,mobilePhone=? where addressId=?";
        return this.update(sql, new PreparedStatementSetter() {
            public void setValues(PreparedStatement pstmt) throws SQLException {
                pstmt.setString(1,address.getName());
                pstmt.setString(2,address.getSite());
                pstmt.setString(3,address.getPostcode());
                pstmt.setString(4,address.getPhone());
                pstmt.setString(5,address.getMobilePhone());
                pstmt.setLong(6,address.getAddressId());
            }
        });
    }

    /*
    * 生成查询通用的Address实例
    * */
    private void createAddress(ResultSet rs) throws SQLException {
        while (rs.next()){
            Address address=new Address();
            address.setAddressId(rs.getLong(1));
            address.setUserId(rs.getInt(2));
            address.setName(rs.getString(3));
            address.setSite(rs.getString(4));
            address.setPostcode(rs.getString(5));
            address.setPhone(rs.getString(6));
            address.setMobilePhone(rs.getString(7));
            list.add(address);
        }
    }
}
